/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Functionality;
import org.ipti.br.tag.entity.Functionality_;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class FunctionalityFacade extends AbstractFacade<Functionality> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FunctionalityFacade() {
        super(Functionality.class);
    }

    @Override
    public void create(Functionality functionality) {
        getEntityManager().persist(functionality);
    }

    @Override
    public void edit(Functionality functionality) {
        functionality.setSerialNumber(null);

        getEntityManager().merge(functionality);
    }

    public void editForSynchronization(Functionality functionality) {
        getEntityManager().merge(functionality);
    }

    public List<Functionality> getFunctionalityChildren(Long functionalityID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Functionality> cq = cb.createQuery(Functionality.class);
        Root<Functionality> from = cq.from(Functionality.class);
        Predicate predicate = cb.equal(from.get(Functionality_.father).get(Functionality_.id), functionalityID);
        cq.where(cb.and(predicate));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
